/* 
 * Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *      * The names of its contributors may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package kameleon.gui.model;

import java.util.Arrays;
import java.util.Date;

/**
 * Subclass of {@code Message} used for generation messages.
 * 
 * <p>Shows the format (as an icon) and the absolute path of the
 * analyzed file as well as the generated files. Displays a different
 * icon for each possible state of the generated icons (waiting,
 * converting, converted or error). Also displays a global message
 * of the current state of the generation.
 * 
 * @author		Schnell Michaël
 * @version		1.0
 */
public class GenerationMessage extends Message {

	/**
	 * Information about the analyzed file and the generated files.
	 */
	protected FileInfo fileInfo ;

	/**
	 * Global state of the generation process.
	 */
	protected GenerationState state ;

	/**
	 * State of the generated files.
	 */
	protected State[] states ;

	/**
	 * Builds an instance with default values.
	 */
	public GenerationMessage() {
		super() ;
		this.state = GenerationState.ANALYZING ;
		this.index = -1 ;
	}// GenerationMessage()

	/**
	 * Builds an instance with the given information 
	 * (files are given the default state waiting).
	 * 
	 * @param 	info
	 * 			information about the generation process
	 */
	public GenerationMessage(FileInfo info) {
		this() ;
		this.fileInfo = new FileInfo(info.getPath()) ;
		this.fileInfo.setIdFormat(info.getIdFormat()) ;
		Date lastGen = info.getLastGeneration() ;
		if (lastGen != null) {
			String [] paths = info.getTargetPath() ;

			this.fileInfo.setLastGeneration(lastGen) ;
			this.fileInfo.setTargetFormatId(info.getTargetFormatId()) ;
			this.setTargetPaths(paths) ;

			this.states = new State[paths.length] ;
			this.initializeStates() ;
		}// if
	}// GenerationMessage(FileInfo)

	/**
	 * Sets the last generation date.
	 * 
	 * @param	generationDate
	 * 			last generation date
	 */
	public void setLastGeneration(Date generationDate) {
		this.fileInfo.setLastGeneration(generationDate) ;
	}// setLastGeneration(Date)

	/**
	 * Sets the ids of the generators used for the last generation.
	 * 
	 * @param	targetIds
	 * 			array containing the ids of the used generators
	 */
	public void setTargetFormatId(String[] targetIds) {
		this.fileInfo.setTargetFormatId(targetIds) ;
	}// setTargetFormatId(String[])

	/**
	 * Sets the state of the file at the given index.
	 * 
	 * @param	index
	 * 			index of the concerned file
	 * 
	 * @param	state
	 * 			new state for the file
	 * 			
	 */
	public void setState(int index, State state) {
		this.states[index] = state;
	}// setState(int, State)

	/**
	 * Sets the state for this generation process.
	 * 
	 * @param 	state
	 * 			new state
	 */
	public void setState(GenerationState state) {
		this.state = state;
	}// setState(GenerationState)

	/**
	 * Sets the absolute paths of the generated files 
	 * (files are given the default state waiting).
	 * 
	 * @param 	targetPaths 
	 * 			Array containing the absolute paths
	 */
	public void setTargetPaths(String[] targetPaths) {
		this.fileInfo.setTargetPaths(targetPaths);
		this.states = new State[targetPaths.length] ;
		this.initializeStates() ;
	}// setTargetPaths(String[])

	/**
	 * Returns the last generation date.
	 * 
	 * @return	Last generation date or {@code null} if 
	 * 			the file was never generated
	 */
	public Date getLastGeneration() {
		return this.fileInfo.getLastGeneration() ;
	}// getLastGeneration()

	/**
	 * Returns the ids of the generators used for the last generation.
	 * 
	 * @return	array containing the ids of the used generators or 
	 * 			{@code null} if the file was never generated
	 */
	public String[] getTargetFormatId() {
		return this.fileInfo.getTargetFormatId() ;
	}// getTargetFormatId()

	/**
	 * Returns the absolute paths of the generated files.
	 * 
	 * @return	Array containing the absolute paths or 
	 * 			{@code null} if the file was never generated
	 */
	public String[] getTargetPath() {
		return this.fileInfo.getTargetPath() ;
	}// getTargetPath()

	/**
	 * Returns the state for this instance.
	 * 
	 * @return	State for this instance
	 */
	public GenerationState getState() {
		return this.state;
	}// getStatus()

	/**
	 * Returns the states of all the generated files.
	 * 
	 * @return	Array containing the states of all 
	 * 			the generated files
	 */
	public State[] getStates() {
		return this.states;
	}// getStates()

	/**
	 * Returns the state of the file at the given index.
	 * 
	 * @param	fileIndex
	 * 			index of the file whose state is requested
	 * 
	 * @return	{@link State} of the file at the given index
	 */
	public State getState(int fileIndex) {
		return this.states[fileIndex] ;
	}// getState(int)

	/**
	 * Returns the id of the analyzer used to analyze the file.
	 * 
	 * @return	Id of the analyzer used to analyze the file
	 */
	public String getIdFormat() {
		return this.fileInfo.getIdFormat() ;
	}// getIdFormat()

	/**
	 * Returns the absolute path of the file attached to this instance.
	 * 
	 * @return	Absolute path of the file attached to this instance
	 */
	public String getPath() {
		return this.fileInfo.getPath() ;
	}// getPath()

	/**
	 * Initializes the states of all the generates files to 
	 * {@link State#WAITING}.
	 */
	protected void initializeStates() {
		if (this.states != null) {
			Arrays.fill(this.states, State.WAITING) ;
		}// if
	}// initializeStates()

	/**
	 * Different states of the generated files during a generation
	 * process.
	 * 
	 * @author	Schnell Michaël
	 * @version	1.0
	 */
	public enum State {
		/**
		 * State indicating that the file is waiting for the
		 * generation to start.
		 */
		WAITING, 
		
		/**
		 * State indicating that the file is currently being
		 * generated.
		 */
		CONVERTING, 
		
		/**
		 * State indicating that the file has been successfully
		 * generated.
		 */
		CONVERTED, 
		
		/**
		 * State indicating that the file could not be generated.
		 */
		ERROR ;
	}// enum State

	/**
	 * Different states of the generation process.
	 * 
	 * @author	Schnell Michaël
	 * @version	1.0
	 */
	public enum GenerationState {
		/**
		 * State indicating that the analyzing process has started but not
		 * finished yet.
		 */
		ANALYZING, 
		
		/**
		 * State indicating that the analyzing process has finished and that the
		 * generation process has started but not finished yet.
		 */
		GENERATING, 
		
		/**
		 * State indicating that the generation process has finished but encountered
		 * an error on some files.
		 */
		PARTIAL_SUCESS, 
		
		/**
		 * State indicating that the generation process has finished, generating
		 * correctly all the specified files.
		 */
		COMPLETE_SUCESS, 
		
		/**
		 * State indicating that the generation process has failed.
		 */
		ERROR ;
	}// enum GenerationState

}// class GenerationMessage